﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_bug_UpdateBug : System.Web.UI.Page
{
    ListItem projectItem;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddlBugId.Items.Clear();
            ltrUpdate.Text = "hide";
            ltrBugSummary.Text = "hide";
            var projects = from item in ApplicationGlobalVariables.KillDaBugsDatabase.User_Projects.Where(i => i.Username == Master.Master.CurrentUserManagement.CurrentUser.Username)
                           select item.Project;

            foreach (Project item in projects)
            {
                projectItem = new ListItem(item.Name, item.ProjectId.ToString());
                ddlProjectName.Items.Add(projectItem);
            }

            // adding items for type of bugs
            foreach (var item in EnumUtility.EnumToList<BugManagement.Type>())
            {
                DDLTypeOfBug.Items.Add(EnumUtility.GetEnumNiceName(item));
            }

            // Adding items for Severity
            foreach (var item in EnumUtility.EnumToList<BugManagement.Severity>())
            {
                dllSeverity.Items.Add(EnumUtility.GetEnumNiceName(item));
            }

            // Adding items for Priority
            foreach (var item in EnumUtility.EnumToList<BugManagement.Priority>())
            {
                dllPriority.Items.Add(EnumUtility.GetEnumNiceName(item));
            }

            // Adding items for reproducabillity
            foreach (var item in EnumUtility.EnumToList<BugManagement.Reproducability>())
            {
                dllReproducability.Items.Add(EnumUtility.GetEnumNiceName(item));
            }
        }
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        Bug bugrecord = (from item in ApplicationGlobalVariables.KillDaBugsDatabase.Bugs.Where(i => i.Summary == ddlBugId.Text)
                         select item).Single();
        BugManagement objBugManagement = new BugManagement(Master.Master.CurrentUserManagement.CurrentUser, bugrecord);

        bool result = objBugManagement.UpdateBugInfo(DDLTypeOfBug.SelectedIndex, textSummary.Text, txtDescription.Text, txtInVersion.Text, txtFoundInComponent.Text, dllSeverity.SelectedIndex, dllReproducability.SelectedIndex, txtReproductionSteps.Text, dllPriority.SelectedIndex);

        if (result == true)
        {
            ltrmess.Text = "Update successfully!";
        }
        else
        {
            ltrmess.Text = "Sorry, database error!";
        }
        ltrBugSummary.Text = "hide";
        ltrUpdate.Text = "hide";
    }

    protected void ddlProjectName_SelectedIndexChanged(object sender, EventArgs e)
    {
        ltrmess.Text = "";
        ddlBugId.Items.Clear();
        var Bugs = from item in ApplicationGlobalVariables.KillDaBugsDatabase.Bugs.Where(i => i.ProjectId == Convert.ToInt32(ddlProjectName.SelectedValue))
                   select item.Summary;

        foreach (var i in Bugs)
        {
            ddlBugId.Items.Add(i);
        }
        ltrBugSummary.Text = "";
    }

    protected void ddlBugId_SelectedIndexChanged(object sender, EventArgs e)
    {
        ltrUpdate.Text = "";
        ltrmess.Text = "";
        Bug bugrecord = (from item in ApplicationGlobalVariables.KillDaBugsDatabase.Bugs.Where(i => i.Summary == ddlBugId.Text)
                         select item).Single();
        DDLTypeOfBug.SelectedIndex = bugrecord.Type;
        textSummary.Text = bugrecord.Summary;
        dllReproducability.SelectedIndex = (int)bugrecord.Reproducability;
        txtInVersion.Text = bugrecord.BugFoundInVersion;
        txtFoundInComponent.Text = bugrecord.BugFoundInComponent;
        txtReproductionSteps.Text = bugrecord.ReproductionSteps;
        dllSeverity.SelectedIndex = bugrecord.Severity;
        dllPriority.SelectedIndex = bugrecord.Priority;
        txtDescription.Text = bugrecord.Description;
        Session["bugid"] = bugrecord.BugId;
    }
}